<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>四则运算计算器</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      max-width: 600px;
      margin: 0 auto;
      padding: 20px;
      background-color: #f5f5f5;
    }
    .calculator {
      background-color: white;
      padding: 20px;
      border-radius: 8px;
      box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
    }
    h1 {
      color: #333;
      text-align: center;
      margin-top: 0;
    }
    .input-group {
      margin-bottom: 15px;
    }
    label {
      display: block;
      margin-bottom: 5px;
      font-weight: bold;
    }
    input, textarea, select {
      width: 100%;
      padding: 10px;
      border: 1px solid #ddd;
      border-radius: 4px;
      box-sizing: border-box;
      font-size: 16px;
    }
    textarea {
      height: 80px;
      resize: none;
    }
    button {
      background-color: #4CAF50;
      color: white;
      border: none;
      padding: 12px;
      border-radius: 4px;
      cursor: pointer;
      font-size: 16px;
      width: 100%;
      margin-top: 10px;
    }
    button:hover {
      background-color: #45a049;
    }
    button.secondary {
      background-color: #f0f0f0;
      color: #333;
      margin-top: 5px;
    }
    button.secondary:hover {
      background-color: #e0e0e0;
    }
    .error {
      color: red;
      margin-top: 10px;
      min-height: 20px;
    }
    .history-container {
      margin-top: 20px;
      border-top: 1px solid #eee;
      padding-top: 15px;
    }
    .history-item {
      padding: 8px;
      border-bottom: 1px solid #eee;
    }
    .history-item:hover {
      background-color: #f9f9f9;
    }
    .flex-container {
      display: flex;
      gap: 10px;
    }
    .flex-container > div {
      flex: 1;
    }
  </style>
</head>
<body>
<div class="calculator">
  <h1>四则运算计算器</h1>

  <div class="input-group">
    <label for="expression">输入运算表达式:</label>
    <input type="text" id="expression" placeholder="例如: (3+5)*2">
  </div>

  <button id="calculate-btn">计算</button>

  <div class="flex-container">
    <div class="input-group">
      <label for="result">当前结果:</label>
      <input type="text" id="result" readonly>
    </div>
    <div class="input-group">
      <label for="history-count">显示条数:</label>
      <select id="history-count">
        <option value="5">5条</option>
        <option value="10" selected>10条</option>
        <option value="20">20条</option>
        <option value="50">50条</option>
      </select>
    </div>
  </div>

  <div id="error-message" class="error"></div>

  <div class="history-container">
    <h3>计算历史</h3>
    <button id="clear-history" class="secondary">清空历史</button>
    <div id="history-list"></div>
  </div>
</div>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
<script>

  //展示表达式链表
  function showlist() {
    $.ajax({
      url: '/calc/getlist',
      type: 'GET',
      dataType: 'json',
      success: function (historyList) {
        $('#history-list').empty();

        if (historyList.length === 0) {
          $('#history-list').append('<p class="empty-message">暂无历史记录</p>');
          return;
        }

        $.each(historyList, function (index, historyItem) {
          $('#history-list').append(
                  $('<div>').addClass('history-item').text(historyItem)
          );
        });
      },
      error: function (xhr, status, error) {
        console.error('获取历史记录失败:', error);
      }
    });
  }

  $("#calculate-btn").click(function () {
    $.ajax({
      type: "post",
      url: "/calc/get",
      data: {
        expreString: $("#expression").val()
      },
      success: function(result){
        $('#result').val(result);
        showlist();
      },
      error: function() {
        alert("表达式有误,请检查如: 除数不能为零、计算符号不正确!")
      }
    });
  });


   $("#clear-history").click(function (){
     $.ajax({
       url: "/calc/deletelist",
       type: "get",
       success: function (result){
         if(result){
           alert("删除成功！")
           showlist();
         }
       }
     });
   });

</script>
</body>
</html>